/**
 * Project Name:WeixinTokenManager
 * File Name:RequestUtil.java
 * Package Name:com.sxhuayuan.util
 * Date:2016年9月18日下午7:21:41
 * Copyright (c) 2016, sxhuayuan.com All Rights Reserved.
 *
 */

package com.sxhuayuan.parking.utils;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:RequestUtil <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年9月18日 下午7:21:41 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 * @see
 */
public class RequestUtil {

	public static Map<String, String> getStringMap(HttpServletRequest request) {
		Map<String, String> bm = new HashMap<String, String>();
		Map<String, String[]> tmp = request.getParameterMap();
		if (tmp != null) {
			for (String key : tmp.keySet()) {
				String[] values = tmp.get(key);
				bm.put(key, values.length == 1 ? values[0].trim() : join(values, ","));
			}
		}
		return bm;
	}

	public static Map<String, String> getStringMap(String request) {
		Map<String, String> bm = new HashMap<String, String>();
		if (request != null) {
			String[] params = request.split("&");
			for (String kv : params) {
				String[] values = kv.split("=");
				bm.put(values[0], values[1]);
			}
		}
		return bm;
	}

	public static String join(String[] values, String separate) {
		StringBuilder sb = new StringBuilder();
		for (String v : values) {
			sb.append(v).append(separate);
		}
		return sb.toString().substring(0, sb.length() - 1);
	}

	public static void main(String[] args) {
		String s = "gmt_payment=2015-06-11 22:33:59&notify_id=42af7baacd1d3746cf7b56752b91edcj34&seller_email=testyufabu07@alipay.com&notify_type=trade_status_sync&sign=kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=&trade_no=2015061121001004400068549373&out_trade_no=21repl2ac2eOutTradeNo322&gmt_create=2015-06-11 22:33:46&seller_id=2088211521646673&notify_time=2015-06-11 22:34:03&subject=FACE_TO_FACE_PAYMENT_PRECREATE中文&trade_status=TRADE_SUCCESS&sign_type=RSA";
		for (Map.Entry<String, String> e : getStringMap(s).entrySet()) {
			System.out.println(String.format("%s=%s", e.getKey(), e.getValue()));
		}
	}
}
